package com.example.madplayer;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ListView;

public class LocalMusicListActivity extends ListActivity {
	
	private List<String> music_list;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.local_audio_list);
		
		music_list = new ArrayList<String>();
		getLocalAudioList();
		LocalMusicAdapter adapter= new LocalMusicAdapter(this, music_list);
		setListAdapter(adapter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
	}
	
	public void getLocalAudioList() {

		ContentResolver resolver = getContentResolver();
		Cursor cursor = resolver.query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		if (cursor.moveToFirst()) {
			for (int i = 0; i < cursor.getCount(); i++) {
				String path;
				cursor.moveToPosition(i);
				cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
				path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
				music_list.add(path);
			}
		}
		cursor.close();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		if( App.isplay ) {
	        Intent intent = new Intent(this, PlayService.class);
	        intent.putExtra(PlayService.PLAY_COMMAND, PlayService.COMMAND_PAUSE);
	        startService(intent);
			App.isplay = false;
		}
		else {
			App.mBinder.setPlayPath("/sdcard/test.mp3");
	        Intent intent = new Intent(this, PlayService.class);
	        intent.putExtra(PlayService.PLAY_COMMAND, PlayService.COMMAND_PLAY);
	        startService(intent);
	        App.isplay = true;
		}
	}
}
